module(..., package.seeall)
local Json = require( "Json" )

function loadData()
	local data
        local path = system.pathForFile( "state.txt", system.ResourceDirectory )
        -- io.open opens a file at path. returns nil if no file found 
        local file = io.open( path, "r" )
        
        if file then
                -- read all contents of file into a string 
                local contents = file:read( "*a" ) 
                data = Json.Decode(contents);
                
                io.close( file )
 		
		return data
	else
		return nil
	end
end

function saveData(data)

        local path = system.pathForFile( "state.txt", system.ResourceDirectory )
        
        -- create file b/c it doesn't exist yet 
        local file = io.open( path, "w" ) 

        if file then
                file:write( Json.Encode(data) ) 
                io.close( file )
        end

end